package com.tfjybj.itoo.exam.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmsdbj.itoo.tool.base.service.impl.BaseServicePlusImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.DisciplineInfoEntity;
import com.tfjybj.itoo.exam.entity.ExamineeEntity;
import com.tfjybj.itoo.exam.model.*;
import com.tfjybj.itoo.exam.provider.dao.DisciplineInfoDao;
import com.tfjybj.itoo.exam.provider.service.DisciplineInfoService;
import com.tfjybj.itoo.exam.provider.service.ExamineeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * DisciplineInfoService接口实现类
 * ${base}表
 *
 * @author 任晓帅
 * @version 2.0.0
 * @since 2.0.0 2018-11-15 19:40:28
 */
@Service("disciplineInfoService")
@Slf4j
public class DisciplineInfoServiceImpl extends BaseServicePlusImpl<DisciplineInfoDao, DisciplineInfoEntity> implements DisciplineInfoService {


    /**
     * 模板生成
     */
    @Resource
    private DisciplineInfoDao disciplineInfoDao;


    @Resource
    private ExamineeService examineeService;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public PageInfo<DisciplineInfoEntity> queryByLikeExaminationId(String examinationId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(disciplineInfoDao.queryLikeExaminationId(examinationId));
    }


    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     *  查看本考场中考生的违纪信息
     * @author      任晓帅
     * @return       List<DisciplineInfoModel> 违纪信息
     * @param       examId 考生id  examRoomId 考场id
     * @since 2.0.0       2019年5月21日15:56:10
     */
    @Override
    public List<DisciplineInfoModel> queryDisciplineStudent(String examId, String examRoomId) {
        return disciplineInfoDao.queryDisciplineStudent(examId,examRoomId);

    }

    /**
     *  查看当前考场中违纪人数
     * @author      王雪芬 任晓帅
     * @return    int 违纪人数
     * @param          examId 考试id examRoomId 考场id
     * @since 2.0.0      2018-11-19 09:40:41
     */
    @Override
    public Integer queryDisciplineCount(String examId,String examRoomId) {
        Integer i;
        List<DisciplineInfoModel> lists = disciplineInfoDao.queryDisciplineCount(examId, examRoomId);
        if (lists.size()>0){
            i=lists.size();
            return i;
        }else {
            return 0;
        }
    }

    /**
     * 查看学生考试的实时成绩和信息
     *
     * @param examRoomId 考场id
     * @return 考生实时信息的集合
     * @author 李青松
     * @since 2.0.0 2018-11-25 17:07:31
     */
    @Override
    public List<RealTimeModel> queryStudentRealTime(String examRoomId, String examId) {
        // 获取到考生信息
        List<RealTimeModel> realTimeModelList = disciplineInfoDao.queryStudentRealTime(examRoomId, examId);
        // 有信息
        if (realTimeModelList.size() > 0) {
            this.select(realTimeModelList);
            //无信息
        } else {
            log.warn("在线监控未获取到考生信息,考场ID为 {},考试ID为 {}" + examRoomId + examId);
        }
        return realTimeModelList;
    }

    /**
     * 根据搜索框查询考场内相应的考生
     *
     * @param studentNamekc
     * @param studentCode
     * @param state
     * @param examRoomId
     * @return ItooResult
     * @author 于云秀
     * @since 2018年12月19日
     */
    @Override
    public List<RealTimeModel> queryStudentInfo(String studentNamekc, String studentCode, String state, String examRoomId, String questionTypeID, String studentClass, String examId) {
        // 获取到考生信息
        List<RealTimeModel> realTimeModelList = disciplineInfoDao.queryStudentInfo(studentNamekc, studentCode, state, examRoomId, questionTypeID, studentClass, examId);
        if (realTimeModelList.size() > 0) {
            this.select(realTimeModelList);
        } else {
            log.warn("在线监控未获取到考生信息,考场ID为 {},考试ID为 {}" + examRoomId + examId);
        }
        return realTimeModelList;
    }


    /**
     * 根据考试id获取该场考试下的所有题型
     *
     * @param examID 考试ID
     * @return 题型分类的集合
     * @author 魏恩君
     * @since 2.0.0 2018/12/22
     */
    @Override
    public List<QuestionTypeModel> selectQuestionTypeByExamID(String examID) {
        return disciplineInfoDao.selectQuestionTypeByExamID(examID);
    }

    /**
     * 根据考生id更新缺考信息
     *
     * @param id         考生id
     * @param absentInfo 缺考信息
     * @return boolean
     * @author 魏恩君
     * @since 2.0.0 2018/12/22
     */
    @Override
    public Boolean addExamMissingInf(String id, String absentInfo) {
        ExamineeEntity examineeEntity = new ExamineeEntity();
        examineeEntity.setAbsentInfo(absentInfo);
        QueryWrapper<ExamineeEntity> examineeQueryWrapper = new QueryWrapper<>();
        examineeQueryWrapper.eq("id", id);
        return examineeService.update(examineeEntity, examineeQueryWrapper);

    }

    /**
     * 在线监控（饼状图）根据考场id查询所有考试学生信息
     *
     * @param examRoomId 考场id
     * @return
     * @author 魏恩君
     * @since 2.0.0 2018/12/23
     */

    @Override
    public List<RealTimeModel> queryAllStudent(String examRoomId, String examId) {
        // 获取到考生信息
        List<RealTimeModel> realTimeModelList = disciplineInfoDao.queryAllStudent(examRoomId, examId);
        if (realTimeModelList.size() > 0) {
            this.select(realTimeModelList);
        } else {
            log.warn("在线监控未获取到考生信息,考场ID为 {},考试ID为 {}", examRoomId, examId);
        }
        return realTimeModelList;
    }


    /**
     * 根据搜索框查询已登录后的考生信息-魏恩君-2018年12月26日
     *
     * @param
     * @return
     * @author 魏恩君
     * @since 2.0.0 2018/12/26
     */
    @Override
    public List<RealTimeModel> queryLoginedInf(String studentNamekc, String studentCode, String state, String examRoomId, String questionTypeID, String studentClass, String examId) {
        // 获取到考生信息
        List<RealTimeModel> realTimeModelList = disciplineInfoDao.queryLoginedInf(studentNamekc, studentCode, state, examRoomId, questionTypeID, studentClass, examId);
        if (realTimeModelList.size() > 0) {
            this.select(realTimeModelList);
        } else {
            log.warn("在线监控未获取到考生信息,考场ID为 {},考试ID为 {}" + examRoomId + examId);
        }
        return realTimeModelList;
    }

    /**
     * 根据搜索框查询未登录的考生信息-魏恩君-2018年12月26日
     *
     * @param
     * @return
     * @author 魏恩君
     * @since 2.0.0 2018/12/26
     */

    @Override
    public List<RealTimeModel> queryNotLoginInf(String studentNamekc, String studentCode, String state, String examRoomId, String questionTypeID, String studentClass, String examId) {
        // 获取到考生信息
        List<RealTimeModel> realTimeModelList = disciplineInfoDao.queryNotLoginInf(studentNamekc, studentCode, state, examRoomId, questionTypeID, studentClass, examId);
        if (realTimeModelList.size() > 0) {
            this.select(realTimeModelList);
        } else {
            log.warn("在线监控未获取到考生信息,考场ID为 {},考试ID为 {}" + examRoomId + examId);
        }
        return realTimeModelList;
    }

    /**
     * 根据考试id查询违纪信息
     *
     * @param studentCode
     * @return
     * @author 于云秀
     * @since 2018年12月27日19:09:33
     */
    @Override
    public List<DisciplineInfoModel> selectWj(String studentCode) {
        return disciplineInfoDao.selectWj(studentCode);
    }

    /**
     * 实时监控简单查询
     *
     * @param search
     * @param examId
     * @param examRoomId
     * @return
     * @author 于云秀
     * @since 2019年1月4日
     */
    @Override
    public List<RealTimeModel> selectExamineeInfo(String search, String examId, String examRoomId) {
        // 获取到考生信息
        List<RealTimeModel> realTimeModelList = disciplineInfoDao.selectExamineeInfo(search, examId, examRoomId);
        if (realTimeModelList.size() > 0) {
            this.select(realTimeModelList);
        } else {
            log.warn("在线监控未获取到考生信息,考场ID为 {},考试ID为 {}" + examRoomId + examId);
        }
        return realTimeModelList;
    }

    /**
     * 查询考生详情
     *
     * @param realTimeModelList
     * @return
     * @author 于云秀
     * @since 2019年4月3日
     */
    public List<RealTimeModel> select(List<RealTimeModel> realTimeModelList) {
        for (RealTimeModel realTimeModel : realTimeModelList) {
            //根据学号去Redis中查询想同的学号
            //  boolean monitorValue = redisTemplate.opsForValue().get("REDI_MONITOR_STUDENTCODE" + realTimeModel.getStudentCode()).equals("true");
            Set<String> monitorValue = redisTemplate.keys("REDI_MONITOR_STUDENTCODE" + realTimeModel.getStudentCode());
            //如果没有setMonitor(false)
            if (CollectionUtils.isEmpty(monitorValue)) {
                realTimeModel.setMonitor(false);
                //如果有setMonitor(monitorValue)
            } else {
                realTimeModel.setMonitor(true);
            }
            //得分率
            int rate = 0;
            //总分为0
            if (realTimeModel.getStandScore() != null) {
                if (realTimeModel.getStandScore() == 0) {
                    rate = 0;
                }
                // 不为0，求得分率（学生得分/总分）
                else {
                    rate = Integer.parseInt(String.valueOf(Math.round((realTimeModel.getSumScore() / realTimeModel.getStandScore()) * 100)));
                }
            }
            // 得分率
            realTimeModel.setScoreRate(rate);
            // 未登录
            String noLogin = "0";
            // 正在考试
            String inExam = "1";
            //交卷
            String handedIn = "3";
            if (noLogin.equals(realTimeModel.getStudentExamState())) {
                realTimeModel.setStudentExamState("未登录");
            }
            if (inExam.equals(realTimeModel.getStudentExamState())) {
                realTimeModel.setStudentExamState("正在考试");
            }
            if (handedIn.equals(realTimeModel.getStudentExamState())) {
                realTimeModel.setStudentExamState("交卷");
            }
        }
        return realTimeModelList;
    }

    /**
     * 查询监考老师
     *
     * @param examId
     * @param examRoomId
     * @return
     * @author 于云秀
     * @since 2019年1月4日
     */
    @Override
    public List<ExamRoomModel> selectTeacherInfo(String examId, String examRoomId) {
        return disciplineInfoDao.selectTeacherInfo(examId, examRoomId);
    }


    /**
     * 批量添加缺考信息
     *
     * @param notLoginIds 未登录考生id
     * @param absentInfo  缺考信息
     * @return boolean
     * @author 魏恩君
     * @since 2.0.0 2019/01/03
     */
    @Override
    public Boolean batchAddNotLogin(String notLoginIds, String absentInfo) {
        // 把数组转成集合
        List<String> listID = Arrays.asList(notLoginIds.split(","));
        return disciplineInfoDao.batchAddNotLogin(listID, absentInfo);
    }


    /**
     * 查询正在考试的考试信息
     *
     * @param operator
     * @return
     * @author cll
     * @since 2019年3月20日
     */
    @Override
    public List<ExaminationInfoModel> selectExaminationInfo(String operator) {
        return disciplineInfoDao.selectExaminationInfo(operator);
    }
}
